package jmu.wj.service.impl;

import jmu.wj.mapper.UserMapper;
import jmu.wj.pojo.User;
import jmu.wj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.sql.Time;
import java.util.Date;

/**
 * @author jxxyw
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
   private UserMapper userMapper;
    @Override
    public void register(User user) {
        //生成用户状态
       User user1= userMapper.findByUsername(user.getUsername());
       if(user1==null){
           user.setStatus("已激活");
           user.setRegisterTime(new Date());
           userMapper.save(user);
       }else{
           throw new RuntimeException("用户名已存在！");
       }
    }

    @Override
    public User login(User user) {
        User user1=userMapper.findByUsername(user.getUsername());
        if(!ObjectUtils.isEmpty(user1)){
            if(user1.getPassword().equals(user.getPassword())){
                return user1;
            }else{
                throw new RuntimeException("密码输入错误！");
            }}
            else {
                throw new RuntimeException("用户名输入错误！");
            }
        }
    }

